<?php
class upload {
	var $_filename; // tên tập tin upload
	var $_maxfilesize; // kích thước tối đa được upload
	var $_fileExtension; //phần mở rộng được phép upload lên
	var $_fileTemp; //tập tin tạm trước khi upload
	var $_uploadDir; //thư mục chứa tập tin upload
	var $_filesize; //thư mục chứa tập tin upload
	
	function __construct($file = 'file'){		
		$this->_filename = $_FILES[$file]['name'];
		$this->_fileTemp = $_FILES[$file]['tmp_name'];
		$this->_filesize = $_FILES[$file]['size'];
	}	
	/*
	 *  Thiết lập kích thước tối đa được upload
	*/
	
	function setFilesize($size){
		$this->_maxfilesize = $size;
	}
	/*
	 *  Thiết lập phần mở rộng cho phép upload lên
	*/
	
	function setFileExtension($ext){
		$this->_fileExtension = $ext;
	}
	/* 
	 *  Thiết lập thư mục upload
	 */
	function setUploadDir($uploadDir){
		$this->_uploadDir = $uploadDir;
	}
	
	function getExtension(){	
		$pattern = '#\.('.$this->_fileExtension.')$#i';
		preg_match($pattern, $this->_filename,$ext);
		return $ext[0];
	}
	/*
	 *  kiểm tra tập tin upload
	*/
	
	function isVal(){
		$flags = false;
		$pattern = '#\.('.$this->_fileExtension.')$#i';		 
		if($this->_filesize < $this->_maxfilesize && preg_match($pattern, $this->_filename)){
			$flags = true;
		}		
		return $flags;
	}
	/*
	 *  Upload
	*/
	function upload($rename = false){
		$source = $this->_fileTemp;		
		if($rename == true){
			$this->_filename = 'file_'.time().$this->getExtension();		
		}
		$dest = $this->_uploadDir . $this->_filename;
		copy($source, $dest);		
	}
	function delete($filename){
		unlink($filename);
	}
	/*
	 *  Resize
	*/
	function resize($originalName, $dirUpload, $dirFix, $widthSize, $heightSize) {
        $original = $dirUpload . $originalName;
   		$fix = $dirFix . $originalName;
        list($width, $height, $type) = getimagesize($original);
        if ($width <= $widthSize && $height <= $heightSize) {
            $ChoiceWith = $width;
            $ChoiceHeight = $height;
        } else {
            if ($width > $height && $width > $widthSize) {
                $radioWidth = $widthSize / $width;
                $ChoiceWith = $radioWidth * $width;
                $ChoiceHeight = $radioWidth * $height;
            }
            if ($width > $height && $width < $widthSize) {
                $ChoiceWith = $width;
                $ChoiceHeight = $height;
            }
            if ($height > $width && $height > $heightSize) {
                $radioHeight = $heightSize / $height;
                $ChoiceWith = $radioHeight * $width;
                $ChoiceHeight = $radioHeight * $height;
            }
            if ($height > $width && $height < $heightSize) {
                $ChoiceWith = $width;
                $ChoiceHeight = $height;
            }
        }
        if ($width == $height) {
            $radioWidth = $widthSize / $width;
            $ChoiceWith = $radioWidth * $width;
            $ChoiceHeight = $radioWidth * $height;
        }
        if ($type == 2) {
          //  @header("Content-type: image/jpeg");
            $tn = imagecreatetruecolor($ChoiceWith, $ChoiceHeight);
            $image = imagecreatefromjpeg($original);
            imagecopyresampled($tn, $image, 0, 0, 0, 0, $ChoiceWith,
                    $ChoiceHeight, $width, $height);
            imagejpeg($tn, $fix, 100);
        }
        if ($type == 1) {
         //   @header("Content-type: image/gif");
            $tn = imagecreatetruecolor($ChoiceWith, $ChoiceHeight);
            $image = imagecreatefromgif($original);
            imagecopyresampled($tn, $image, 0, 0, 0, 0, $ChoiceWith,
                    $ChoiceHeight, $width, $height);
            imagegif($tn, $fix);
        }
        if ($type == 3) {
          //  @header("Content-type: image/png");
            $tn = imagecreatetruecolor($ChoiceWith, $ChoiceHeight);
            $image = imagecreatefrompng($original);
            imagecopyresampled($tn, $image, 0, 0, 0, 0, $ChoiceWith,
                    $ChoiceHeight, $width, $height);
            imagepng($tn, $fix, 100);
        }
    }
}